package com.pagoda.nerp.trade.service.impl;

import com.pagoda.nerp.trade.dao.AccRoleDao;
import com.pagoda.nerp.trade.entity.AccRole;
import com.pagoda.nerp.trade.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2016/11/24.
 */
@Service
public class RoleServiceImpl implements RoleService {


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AccRoleDao roleDao;


    /**
     * 新增用户角色
     * @param roleInfo
     * @return
     */
    @Transactional
    public int addRole(AccRole roleInfo) {

        return roleDao.insert(roleInfo);

    }

    /**
     * 根据roleId更新角色
     * @param accRoleInfo
     * @return
     */
    @Transactional
    public int updateRole(AccRole accRoleInfo) {

        return roleDao.updateByPrimaryKeySelective(accRoleInfo);

    }

    /**
     * 查询角色列表 （roleName)
     * @param roleName
     * @return
     */
    public List<AccRole> queryRoleListByRoleName(String roleName) {
        roleName = "%"+roleName+"%";
        return roleDao.queryRoleListByRoleName(roleName);

    }

    /**
     * 查询单个角色
     * @param map
     * @return
     */
    public AccRole queryRoleByRoleIdOrRoleCode(Map<String,String> map) {
        AccRole roleInfo = null;

        if (map.get("roleId") != null) {
            roleInfo = roleDao.queryRoleByRoleId(Integer.valueOf(map.get("roleId")));
        }else if (map.get("roleCode") != null) {
            roleInfo = roleDao.queryRoleByRoleCode(map.get("roleCode"));
        }

        return roleInfo;
}

    /**
     * 通过roleId查询角色
     * @param roleId
     * @return
     */
    public AccRole queryRoleByRoleId(int roleId) {

        return roleDao.queryRoleByRoleId(roleId);
    }


}
